import java.util.Random;
import java.util.Scanner;

public class GuessTheRightNumber {
    public static void guess() {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int targetNumber = random.nextInt(100) + 1; // 生成 1 到 100 的随机数
        int attempts = 0;

        System.out.println("欢迎来到猜数字游戏！");
        System.out.println("我已经选定了一个 1 到 100 之间的数字，你能猜出来吗？");

        while (true) {
            System.out.print("请输入你的猜测（输入 0 退出游戏）：");
            int guess = scanner.nextInt();
            attempts++;

            if (guess == 0) {
                System.out.println("游戏已退出！感谢你的参与！");
                break;
            }

            if (guess < targetNumber) {
                System.out.println("太小了，再试试！");
            } else if (guess > targetNumber) {
                System.out.println("太大了，再试试！");
            } else {
                System.out.println("恭喜你，猜对了！数字是：" + targetNumber);
                System.out.println("你总共尝试了 " + attempts + " 次。");
                break;
            }
        }

        scanner.close();
    }
}
